﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.IDAL;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.DAL;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.BLL
{
    public class UserManager
    {
        private IUserAccess _db;

        public UserManager()
        {
            _db = DalFactory.CreateUserAccess();
        }

        public List<UserInfo> GetUsers()
        {
            return GetUsers(string.Empty);
        }

        public List<UserInfo> GetUsers(string filter)
        {
            return _db.GetUsers(filter);
        }

        public UserInfo GetUserById(int id)
        {
            return _db.GetUser(id);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="newUser"></param>
        /// <param name="errString"></param>
        /// <returns></returns>
        public bool AddUser(UserInfo newUser,out string errString)
        {
            errString = string.Empty;
            List<UserInfo> checkUsers;

            if (newUser.LogName.Length <= 0)
            {

                errString = "登录名必须输入";
                return false;
            }
            if (newUser.Name.Length <= 0)
            {

                errString = "登录名必须输入";
                return false;
            }
            if (newUser.Passwords.Length < 6)
            {
                errString = "登录密码应大于6个字符少于20字符";
                return false;
            }

            checkUsers = GetUsers(" FlogName ='" + newUser.LogName+"'");
            if (checkUsers.Count > 0)
            {
                errString = "当前登录名已经被占用";
                return false;
            }
           
            newUser.Passwords = PublicFunction.GetMd5(newUser.Passwords.ToLower());

            return _db.AddUser(newUser);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteUser(int id)
        {
            UserInfo curUser = _db.GetUser(id);
            if (curUser != null)
            {
                if (curUser.LogName.Trim().ToLower() == "admin")//内置用户不允许删除
                {
                    return false;
                }
            }
            return _db.DeleteUser(id);
        }


        /// <summary>
        /// 改密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool ChangePwd(int id,string pwd,out string errString)
        {
            errString = string.Empty;

            if (pwd.Length < 6)
            {
                errString = "密码必须大于6位!";
                return false;
            }

            return _db.ChangePwd(id, PublicFunction.GetMd5(pwd.ToLower()));
        }

        /// <summary>
        /// 更新当前用户信息
        /// </summary>
        /// <param name="curInfo"></param>
        /// <param name="errString"></param>
        /// <returns></returns>
        public bool ChangeUserInfo(UserInfo curInfo, out string errString)
        {
            errString = string.Empty;

            bool isOk= _db.Update(curInfo, out errString);
            if ((!isOk)&& string.IsNullOrEmpty(errString))
            {
                errString = "修改密码时出现未知异常,原因:网络错误";
            }

            return isOk;
        }

    }
}
